package com.example.common.widget.state;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.common.BuildConfig;
import com.example.common.R;

import static com.example.common.widget.state.NetworkStateLayout.ERROR_NETWORK_STATE;
import static com.example.common.widget.state.NetworkStateLayout.LOADING_NETWORK_STATE;
import static com.example.common.widget.state.NetworkStateLayout.NOT_NETWORK_STATE;

public class ErrorNetworkStateView extends BaseStateView {

    private OnAnewRequestNetworkListener onAnewRequestNetworkListener;

    @Override
    public Integer getStateCode() {
        return ERROR_NETWORK_STATE;
    }

    @Override
    public int getLayoutId() {
        return R.layout.state_error_network;
    }

    @Override
    public void bindParentView(NetworkStateLayout parentView) {
        super.bindParentView(parentView);

        getNetworkStateView()
                .findViewById(R.id.request_network)
                .setOnClickListener(view -> {
                    parentView.showNetworkStateView(LOADING_NETWORK_STATE);
                    new Handler(Looper.myLooper()).postDelayed(() ->
                                    onAnewRequestNetworkListener.onAnewRequestNetwork(),
                            2000);
                });
    }

    @Override
    public void showView(CharSequence message) {
        super.showView(message);
        if(!TextUtils.isEmpty(message)){
            TextView textView = getNetworkStateView().findViewById(R.id.text_view);
            textView.setText(message);
        }
    }

    @Override
    public void setOnAnewRequestNetworkListener(OnAnewRequestNetworkListener onAnewRequestNetworkListener){
        this.onAnewRequestNetworkListener = onAnewRequestNetworkListener;
    }
}
